<?



/**
 * class MVideos
 * 
 */
class MVideos_vidwiz extends Model_vidwiz {

  public $allowedMIMEs = array(
        'mp2' => 'video/mpeg',
        'mpeg' => 'video/mpeg',
        'mpg' => 'video/mpeg',
        'avi' => 'video/x-msvideo',
        'mp4' => 'video/mp4',
        'flv' => 'video/x-flv',
        'f4v' => 'video/x-flv',
    );


  /**
   * 
   *
   * @return 
   * @access public
   */
    public function __construct() {
        global $config;
        $this->tableName = $config['tables']['videos'];
        $this->installationFields = array(
                  array(
                      0 => 'id',
                      1 => 'int',
                      2 => 'NOT NULL AUTO_INCREMENT PRIMARY KEY'
                  ),
                  array(
                      0 => 'publisher_username',
                      1 => 'TEXT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'description',
                      1 => 'TEXT',
                      2 => ''
                  ),
                  array(
                      0 => 'upload_date',
                      1 => 'DATETIME',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'title',
                      1 => 'TEXT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'metatags',
                      1 => 'TEXT',
                      2 => ''
                  ),
                  array(
                      0 => 'file_position',
                      1 => 'TEXT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => "thumb",
                      1 => "TEXT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "category",
                      1 => "INT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "converted",
                      1 => "BOOLEAN",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "breaking",
                      1 => "BOOLEAN",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "post_id",
                      2 => "TEXT",
                      3 => ""
                  )
                );
    }

  /**
   * 
   *
   * @return array I MIMEs ammessi
   * @access public
   */
  public function getAllowedMIMEs( ) {
    return $this->allowedMIMEs;
  } // end of member function getAllowedMIMEs

  /**
   * 
   * Estrae un fotogramma dal video passato per parametro, salvando l'immagine
   * nella stessa cartella del video
   * @param string _videoPath percorso del video da dove prendere le miniature

   * @return string l'indirizzo dell'immagine miniatura
   * @access public
   */
  public function getThumbFromVideo( $videoPath, $videoPathDB ) {
      $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
      global $config;
      $time =  exec($config['ffmpeg_path']."ffmpeg -i ".$videoPath." 2>&1 | grep \"Duration\" | cut -d ' ' -f 4 | sed s/,//");  
      $duration = explode(":",$time);  
      $duration = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);  
      $duration = round($duration/2);
      $output = $this->getFileInfo($videoPath);
      $output = $output['dirname']."/".$output['filename'].".png";
      exec($config['ffmpeg_path']."ffmpeg -vframes 1 -ss $duration -i $videoPath -f image2 $output");
      $this->createImageResized($output,134);
      $videoPathDB = preg_replace($config['patterns']['extension'],".png", $videoPathDB);
      return $videoPathDB;
  } // end of member function getThumbFromVideo


   /**
    * A partire dall'id passato per parametro, ritorna tutte le informazioni relative
    * al video con quell'id
    * @global array $config
    * @param string $id l'id di un video
    * @return type 
    */
    public function getVideoFromId($id){
        global $config;
        $query = $this->searchQuery($config['tables']['videos'], array(array("id","=",$id)));
        return $this->getRowsAssoc($query);
    }
    
    /**
    * A partire dal path passato per parametro, ritorna tutte le informazioni relative
    * al video con quel path
    * @global array $config
    * @param string $path il path di un video
    * @return array il video con quel path se esiste, false altrimenti 
    */
    public function getVideoFromPath($path){
        global $config;
        $query = $this->searchQuery($config['tables']['videos'], array(array("file_position","=",$path)));
        return $this->getRowsAssoc($query);
    }
    
    public function checkValidBreakingNews(){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        global $config;
        $query = $this->searchQuery($config['tables']['videos'], array(
            array("converted","=","1"),
            array("breaking","=","1")));
        $results = $this->getRowsAssoc($query);
        foreach($results as $key=>$value){
            $date = $value['upload_date'];
            $now = strtotime(date("Y-m-d H:i:s"));
            $newdate = strtotime ('+30 minutes' , strtotime ($date)) ;
            if($now>$newdate){
            $query = $this->updateQuery($config['tables']['videos'], 
                    array(array('breaking','FALSE')), array(
                        array('id',">",$id),
                        array('breaking','=','1'),
                        array('converted','=','1')));
            $this->executeQuery($query);
            
            }}
    }


} // end of MVideos
?>
